<?php if(!defined('BASEPATH')) exit('No direct script access allowed');

class Posts extends CI_Model {
    
    
    function __construct() {
        parent::__construct();    
    }
    
    public function selectPosts() {
        $this->db->select('id, title, content, posted_on');
        $this->db->order_by('id', 'desc');
        $query = $this->db->get('posts');
        if($query->num_rows() > 0){
            $rows = array();
            foreach($query->result() as $row) {
                $rows[] = $row;
            }
            return $rows; 
        }  
    }
    
    public function deletePost($id) {
        $this->db->query("DELETE FROM posts WHERE id = '".$id."'");
    }
    
    public function editPost($id) {
        $query = $this->db->select('id, title, content')->from('posts')->where('id', $id)->limit(1)->get();
            if($query->num_rows() > 0){
                $row = $query->row();
                return $row;
            }
    }
    
}